﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using iSun.DAL;
using OpenExpressApp.ORM;

namespace iSun.Entity
{
    [Serializable]
    [Table(Name="DeptInfo")]
    [iSun.Model.Table("DeptInfo","Id")]
    public class DeptInfo : iSun.Entity.EntityBase //源自业务基类,泛型提供强类型的Save及Clone
    {
        #region Business Methods 业务方法
        private static PropertyInfo<int> IdProperty = RegisterProperty(new PropertyInfo<int>("Id"));
        
        [Column, PK]
        public int Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }

        private static PropertyInfo<int> GsidProperty =
      RegisterProperty(new PropertyInfo<int>("Gsid"));
        [Column]
        public int Gsid
        {
            get { return GetProperty(GsidProperty); }
            set { SetProperty(GsidProperty, value); }
        }

        /// <summary>
        /// ID和GsId整合
        /// </summary>
        public string IdAndGsId
        {
            get { return Id + "|" + Gsid; }
        }

        /// <summary>
        /// Bmname
        /// </summary>
        private static PropertyInfo<string> BmnameProperty =
           RegisterProperty(new PropertyInfo<string>("Bmname"));
        [Column]
        public string Bmname
        {
            get { return GetProperty(BmnameProperty); }
            set { SetProperty(BmnameProperty, value); }
        }

        /// <summary>
        /// Bmcdate
        /// </summary>
        private static PropertyInfo<string> BmcdateProperty =
           RegisterProperty(new PropertyInfo<string>("Bmcdate"));
        [Column]
        public string Bmcdate
        {
            get { return GetProperty(BmcdateProperty); }
            set { SetProperty(BmcdateProperty, value); }
        }

        /// <summary>
        /// Bmunitid
        /// </summary>
        private static PropertyInfo<string> BmunitidProperty =
          RegisterProperty(new PropertyInfo<string>("Bmunitid"));
        [Column]
        public string Bmunitid
        {
            get { return GetProperty(BmunitidProperty); }
            set { SetProperty(BmunitidProperty, value); }
        }

        /// <summary>
        /// Bmfzr
        /// </summary>
        private static PropertyInfo<string> BmfzrProperty =
        RegisterProperty(new PropertyInfo<string>("Bmfzr"));
        [Column]
        public string Bmfzr
        {
            get { return GetProperty(BmfzrProperty); }
            set { SetProperty(BmfzrProperty, value); }
        }

        /// <summary>
        /// Bmtel
        /// </summary>
        private static PropertyInfo<string> BmtelProperty =
        RegisterProperty(new PropertyInfo<string>("Bmtel"));
        [Column]
        public string Bmtel
        {
            get { return GetProperty(BmtelProperty); }
            set { SetProperty(BmtelProperty, value); }
        }

        /// <summary>
        /// Bmfax
        /// </summary>
        private static PropertyInfo<string> BmfaxProperty =
        RegisterProperty(new PropertyInfo<string>("Bmfax"));
        [Column]
        public string Bmfax
        {
            get { return GetProperty(BmfaxProperty); }
            set { SetProperty(BmfaxProperty, value); }
        }

        /// <summary>
        /// Bmhelpcode
        /// </summary>
        private static PropertyInfo<string> BmhelpcodeProperty =
        RegisterProperty(new PropertyInfo<string>("Bmhelpcode"));
        [Column]
        public string Bmhelpcode
        {
            get { return GetProperty(BmhelpcodeProperty); }
            set { SetProperty(BmhelpcodeProperty, value); }
        }

        /// <summary>
        /// Bmlevel
        /// </summary>
        private static PropertyInfo<string> BmlevelProperty =
        RegisterProperty(new PropertyInfo<string>("Bmlevel"));
        [Column]
        public string Bmlevel
        {
            get { return GetProperty(BmlevelProperty); }
            set { SetProperty(BmlevelProperty, value); }
        }

        /// <summary>
        /// Upgsid
        /// </summary>
        private static PropertyInfo<int> UpgsidProperty =
        RegisterProperty(new PropertyInfo<int>("Upgsid"));
        [Column]
        public int Upgsid
        {
            get { return GetProperty(UpgsidProperty); }
            set { SetProperty(UpgsidProperty, value); }
        }

        /// <summary>
        /// Isvalue
        /// </summary>
        private static PropertyInfo<int> IsvalueProperty =
        RegisterProperty(new PropertyInfo<int>("Isvalue"));
        [Column]
        public int Isvalue
        {
            get { return GetProperty(IsvalueProperty); }
            set { SetProperty(IsvalueProperty, value); }
        }

        /// <summary>
        /// Explain
        /// </summary>
        private static PropertyInfo<string> ExplainProperty =
        RegisterProperty(new PropertyInfo<string>("Explain"));
        [Column]
        public string Explain
        {
            get { return GetProperty(ExplainProperty); }
            set { SetProperty(ExplainProperty, value); }
        }

        /// <summary>
        /// Userid
        /// </summary>
        private static PropertyInfo<int> UseridProperty =
        RegisterProperty(new PropertyInfo<int>("Userid"));
        [Column]
        public int Userid
        {
            get { return GetProperty(UseridProperty); }
            set { SetProperty(UseridProperty, value); }
        }


        /// <summary>
        /// Addtime
        /// </summary>
        private static PropertyInfo<string> AddtimeProperty =
        RegisterProperty(new PropertyInfo<string>("Addtime"));
        [Column]
        public string Addtime
        {
            get { return GetProperty(AddtimeProperty); }
            set { SetProperty(AddtimeProperty, value); }
        }
        #endregion

        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            ValidationRules.AddRule(CommonRules.StringRequired, "Bmname");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Bmname", 50));
            
            //base.AddBusinessRules();
        }

        /// <summary>
        /// 验证规则与业务对象
        /// </summary>
        /// <returns></returns>
        public override string CheckData()
        {
            return base.CheckData();
        }

        #endregion

        #region Authorization Rules 授权规则

        #endregion
        
        #region Factory Methods 工厂方法
        public static DeptInfo New()
        {
            return DataPortal.Create<DeptInfo>();
        }
        public static DeptInfo Get(string id)
        {
            return DataPortal.Fetch<DeptInfo>(id);
        }
        public static void Delete(string id)
        {
            DataPortal.Delete(id);
        }

        //public override DeptInfo Save()
        //{
        //    return base.Save();
        //}
        #endregion

        #region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(string id)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
             using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("Id").Equal(id);
                var data = db.Select<DeptInfo>(q)[0];
                LoadProperty(IdProperty, data.Id);
                LoadProperty(GsidProperty, data.Gsid);
                LoadProperty(BmnameProperty, data.Bmname);
                LoadProperty(BmcdateProperty, data.Bmcdate);
                LoadProperty(BmunitidProperty, data.Bmunitid);
                LoadProperty(BmfzrProperty, data.Bmfzr);
                LoadProperty(BmtelProperty, data.Bmtel);
                LoadProperty(BmfaxProperty, data.Bmfax);
                LoadProperty(BmhelpcodeProperty, data.Bmhelpcode);
                LoadProperty(UpgsidProperty, data.Upgsid);
                LoadProperty(IsvalueProperty, data.Isvalue);
                LoadProperty(ExplainProperty, data.Explain);
                LoadProperty(UseridProperty, data.Userid);
                LoadProperty(AddtimeProperty, data.Addtime);
            }
        }


   

        #endregion

        #region Exists 
        #endregion
    }
}
